package com.nxbw.quality.application.service.impl;

import com.nxbw.quality.application.mapper.ApplicationMapper;
import com.nxbw.quality.application.pojo.dto.ApplicationQuery;
import com.nxbw.quality.application.pojo.dto.ApplicationSaveParam;
import com.nxbw.quality.application.pojo.po.Application;
import com.nxbw.quality.application.service.ApplicationService;
import com.nxbw.quality.base.response.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public PageData selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请: {}",applicationQuery);
        List<Application> applications = applicationMapper.selectList(applicationQuery);
        log.debug("查询申请结果: {}",applications);
        PageData pageData = new PageData();
        pageData.setList(applications);
        pageData.setTotal(applicationMapper.selectCount(applicationQuery));
        return pageData;
    }

    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请: {}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        if (applicationSaveParam.getId() == null) {
            application.setStatus("104");
            application.setApplicationTime(new Date());
            application.setCreateTime(new Date());
            applicationMapper.insert(application);
        } else {
            application.setUpdateTime(new Date());
            applicationMapper.update(application);
        }
    }

    @Override
    public void deleteApplication(Integer id) {
        log.debug("删除申请: {}",id);
        applicationMapper.delete(id);
    }
}
